package com.sc.portal.controller;

import com.sc.common.pojo.CartItem;
import com.sc.common.pojo.CommonResult;
import com.sc.pojo.TbUser;
import com.sc.portal.pojo.OrderInfo;
import com.sc.portal.service.CartService;
import com.sc.portal.service.OrderService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @auther sc
 * @Date 2017/11/8
 */
@RequestMapping("/order")
@Controller
public class OrderController {

    @Autowired
    private CartService cartService;
    @Autowired
    private OrderService orderService;
    @RequestMapping("/order-cart")
    public String showCartOrder(Model model, HttpServletRequest request){
        List<CartItem> cartItems = cartService.listCart(request);
        model.addAttribute("cartList",cartItems);
        return "order-cart";
    }

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public String  createOrder(OrderInfo orderInfo,Model model,
                               HttpServletRequest request){
        TbUser user = (TbUser) request.getAttribute("user");
        orderInfo.setUserId(user.getId());
        orderInfo.setBuyerNick(user.getUsername());
        String orderId = orderService.createOrder(orderInfo);
        model.addAttribute("orderId",orderId);
        model.addAttribute("payment",orderInfo.getPayment());
        DateTime d = new DateTime();
        DateTime dateTime = d.plusDays(3);
        model.addAttribute("date",dateTime.toString("yyyy-MM-dd"));
        return "success";
    }

}
